package com.chenfeng.ego.search.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.chenfeng.ego.commons.pojo.ItemSearch;
import com.chenfeng.ego.commons.pojo.SearchResult;
import com.chenfeng.ego.search.mapper.SolrMapper;
import com.chenfeng.ego.search.service.SolrService;
@Service
public class SolrServiceImpl implements SolrService
{
	@Resource
	private SolrMapper mapper;
	@Value("${zkHost}")
	private String zkHost;
	@Override
	public String initSolr() throws SolrServerException, IOException
	{
		List<ItemSearch> list = mapper.seAll();
		CloudSolrServer server = new CloudSolrServer(zkHost);
		server.setDefaultCollection("collection1");
		for (ItemSearch itemSearch : list)
		{
			SolrInputDocument doc = new SolrInputDocument();
			doc.addField("id", itemSearch.getId());
			doc.addField("item_category_name", itemSearch.getItem_category_name());
			doc.addField("item_desc", itemSearch.getItem_desc());
			doc.addField("item_image", itemSearch.getItem_image());
			doc.addField("item_price", itemSearch.getItem_price());
			doc.addField("item_sell_point", itemSearch.getItem_sell_point());
			doc.addField("item_title", itemSearch.getItem_title());
			server.add(doc);
		}
		server.commit();
		return "ok";
	}
	@Override
	public SearchResult selByQ(String q, int page, int rows) throws SolrServerException
	{
		SearchResult sear = new SearchResult();
		//连接集群
		CloudSolrServer server = new CloudSolrServer(zkHost);
		//设置默认集群
		server.setDefaultCollection("collection1");
		//设置查询条件
		SolrQuery query = new SolrQuery();
		query.setQuery("item_keywords:"+q);
		//设置查询的从第几条开始
		query.setStart((page-1)*rows);
		//设置查询每页显示多少条
		query.setRows(rows);
		//查询条件高亮显示
		query.setHighlight(true);
		//设置高亮显示的参数
		query.addHighlightField("item_title item_sell_point");
		//设置高亮显示的前缀
		query.setHighlightSimplePre("<span style='color:red'>");
		//结束
		query.setHighlightSimplePost("</span>");
		QueryResponse response = server.query(query);
		//将数据放入solr集合中
		SolrDocumentList list = response.getResults();
		long count = list.getNumFound();
		//设置返回数据的总页数
		sear.setTotalPages(count%rows==0?count/rows:count/rows+1);
		//第几页
		sear.setPage(page);
		List<ItemSearch> listResult = new ArrayList<>();
		//map集合
		Map<String,Map<String,List<String>>> map = response.getHighlighting();
		//循环solr集合
		for(SolrDocument doc:list)
		{
			ItemSearch is = new ItemSearch();
			is.setId(Long.parseLong(doc.get("id").toString()));
			is.setItem_category_name(doc.get("item_category_name").toString());
			is.setItem_image(doc.get("item_image").toString() == null ? "" : doc.get("item_image").toString().split(",")[0]);
			is.setItem_price(Long.parseLong(doc.get("item_price").toString()));
			
			Map<String,List<String>> map2 = map.get(doc.get("id").toString());
			if (map2 != null && map2.size() > 0) {
				if (map2.get("item_sell_point") != null && map2.get("item_sell_point").size() > 0) {
					is.setItem_sell_point(map2.get("item_sell_point").get(0));
				} else {
					is.setItem_sell_point(doc.get("item_sell_point").toString());
				}
			} else {
				is.setItem_sell_point(doc.get("item_sell_point").toString());
			}

			if (map2 != null && map2.size() > 0) {
				if (map2.get("item_title") != null && map2.get("item_title").size() > 0) {
					is.setItem_title(map2.get("item_title").get(0));
				} else {
					is.setItem_title(doc.get("item_title").toString());
				}
			} else {
				is.setItem_title(doc.get("item_title").toString());
			}
			listResult.add(is);
		}
	    sear.setItemList(listResult);
		return sear;
	}
}
